<?php
$section = "home";
$titre = _LB_ACCUEIL;
$link = $section;
Session::setBackLink($link);
//create cfer
$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
));
$booking  = new Booking();
$booking->STATUS = 0;
$booking->find();
$numberBooking = $booking->N;

$testiminial  = new Testimonial();
$testiminial->STATUS = 0;
$testiminial->find();
$numberTestimonial = $testiminial->N;

$sql = "SELECT COUNT(*) AS NUMBER_ONLINE FROM USER_ONLINE WHERE ACTIVITY > ". (time() - 600);
$uOnline = new Member();
$uOnline->query($sql);
$uOnline->fetch();


$bookList = Booking_detail::getbookingDetail(0);
$cData = array();
foreach($bookList as $bookingID =>$data){
	//echo json_encode($data);die();
	foreach($data["TDATA"] as $booking){
		$parseData = array(); 
		$parseData["booking_id"] = $bookingID;
		$parseData["status"] = $data['STATUS'];
		$parseData["title"] = $data["USER_INFO"]["FIRST_NAME"]. " " . $data["USER_INFO"]["LAST_NAME"] ." " . date("H:i", strtotime($booking["BOOKING_DATE"]));
		$parseData["start"] = date("Y, m-1, j, G, i", strtotime($booking["BOOKING_DATE"]));
        $parseData["id_tech"] = $booking["ID_TECHNICIAN"];
		$time = 0;
		$serviceText = "";
		foreach($booking["SDATA"] as $service){
			$time = $time + $service["TIME"];
			$serviceText .= $service["TITLE"]. "\\n"; 
		}
		$parseData["serviceText"] = $serviceText;
		$parseData["end"] = date("Y, m-1, j, G, i", strtotime($booking["BOOKING_DATE"]) + $time*60);
		$cData[] = $parseData;
 	}	
}
$test = "";
foreach($cData as $item){
	$test .= "
			{
				title : '".$item['title']."',
				start : new Date(".$item['start']."),
				end : new Date(".$item['end']."),
				allDay : false,
				url : '". APPLICATION_URL . "/booking/detail/?id={$item['booking_id']}',
				". ($item['status'] == 2? "className : 'customReject',":"")."
				tooltip : '".$item["serviceText"]."',
                resourceId: ". $item['id_tech'] ."
			 },
			";
}
$test =  "[{$test}]";

$techJson = "";
$tech = new Technician();
$tech->DISABLED = 0;
$tech->find();
while($tech->fetch()){
    $techJson .= "
              {
                name: '".$tech->TITLE."',
                id:	{$tech->ID}
              },
             ";
}
$techJson .= "
              {
                name: 'None',
                id: 0
              }
             ";

Session::setBackCfer($link, $cfer->getCferArray());
$smarty->assign('cfer', $cfer);
$smarty->assign('numberBooking', $numberBooking);
$smarty->assign('numberTestimonial', $numberTestimonial);
$smarty->assign('NUMBER_ONLINE',$uOnline->NUMBER_ONLINE);
$smarty->assign('jsonData', $test);
$smarty->assign('jsonTech', $techJson);
$smarty->display("home.html");